<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 判断一个标签是否是html标签
        let tags = 'div,p,span,img,ul,li'.split(',')
        //一般实现 :每次验证都要重新遍历数组
        function isHTML(str){
            for(let i = 0; i < tags.length; i++){
                if(tags[i] === str) return true
            }
            return false
        }
        // 使用柯里化，进行缓存本质上是闭包
        function makeMap(tags){
            let cache = {}
            tags.forEach(item=>cache[item] = true)
            return function(str){
                return !!cache[str]
            }
        }
        let a = 'div'
        let htmlCache = makeMap(tags)
        // console.log(htmlCache(a))


        // vue源码的缓存功能模块
        /**
 * Create a cached version of a pure function.
 */
// 高阶函数
var cache 
function cached(fn){
    cache = Object.create(null)
    return function(str){
        // 如果有当前这个值就直接返回，没有的话就缓存起来再返回
        return cache[str] || (cache[str] = fn(str))
    }
}

/**
 * Camelize a hyphen-delimited string.
 */
// 功能将el-button或者是v-if这样的串用驼峰的方式缓存起来
//elButton vIf
const complileRE = /-(\w)/g
const complile = cached((str)=>{
    return str.replace(complileRE, (_, c)=> c ? c.toUpperCase : '')
})
/**
 * Capitalize a string.
 */
// 将单词变成首字母大写
const capitalize = cached((str) => {
  return str.charAt(0).toUpperCase() + str.slice(1)
})

/**
 * Hyphenate a camelCase string.
 */
// HelloWorld -》 hello-world
const hyphenateRE = /\B([A-Z])/g
const hyphenate = cached((str) => {
  return str.replace(hyphenateRE, '-$1').toLowerCase()
})
    </script>
</body>
</html>